﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WaveAnimation.Controls
{
	public class WaveButton : Button
	{
		//角度
		public CornerRadius CornerRadius
		{
			get { return (CornerRadius)GetValue(CornerRadiusProperty); }
			set { SetValue(CornerRadiusProperty, value); }
		}

		public static readonly DependencyProperty CornerRadiusProperty =
			DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(WaveButton), new PropertyMetadata(new CornerRadius()));

		public SolidColorBrush RingColorBackgound
		{
			get { return (SolidColorBrush)GetValue(RingColorBackgoundProperty); }
			set { SetValue(RingColorBackgoundProperty, value); }
		}

		public static readonly DependencyProperty RingColorBackgoundProperty =
			DependencyProperty.Register("RingColorBackgound", typeof(SolidColorBrush), typeof(WaveButton), new PropertyMetadata(Brushes.Green));

		public WaveButton()
		{
			this.Click += WaveSpreadAnimationEvent;
		}

		private void WaveSpreadAnimationEvent(object sender, RoutedEventArgs e)
		{
			var path = (Path)this.Template.FindName("waveShape", this);
			var ring = (EllipseGeometry)this.Template.FindName("waveRing", this);
			var mousePoint = Mouse.GetPosition(this);//鼠标距离控件左上原点的坐标

			var originRadius = this.Height / 2;
			var spreadedRadius = this.Width * 1.25;

			ring.Center = mousePoint;

			var dur = 500d;
			Duration halfDuration = new Duration(TimeSpan.FromMilliseconds(dur / 2));
			Duration wholeDuration = new Duration(TimeSpan.FromMilliseconds(dur / 2));

			var ringOpacity = new DoubleAnimation(0d, 1d, halfDuration) { AutoReverse = true };
			var ringRadius = new DoubleAnimation(originRadius, spreadedRadius, wholeDuration)
			{
				EasingFunction = new CubicEase()
				{ EasingMode = EasingMode.EaseInOut }
			};

			path.BeginAnimation(Path.OpacityProperty, ringOpacity);
			ring.BeginAnimation(EllipseGeometry.RadiusXProperty, ringRadius);
		}

		static WaveButton()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(WaveButton), new FrameworkPropertyMetadata(typeof(WaveButton)));
		}
	}
}
